package com.zb.mobile.network.controller;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.annotation.SystemLog;
import com.zb.constant.LogConstant;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.AccountLoginToken;
import com.zb.exception.SMSException;
import com.zb.mobile.NetworkMobileEmpVo;
import com.zb.mobile.NetworkRegisterAccountVo;
import com.zb.mobile.UserMobileInfoVo;
import com.zb.mobile.network.service.ConNetworkAccountMobileService;
import com.zb.redis.RedisNetworkLoginTokenUtil;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.stp.StpNetworkUtil;
import com.zb.utils.TokenNetworkUtil;
import com.zb.utils.VerificUtil;
import com.zb.vo.LoginVo;
import com.zb.vo.UserContactVo;
import com.zb.vo.UserPasswordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author WEN
 * @Date 2023/11/17 0017
 **/
@Api(tags = "服务商移动端账号类接口")
@RestController
@RequestMapping("/mobile/network/account")
public class NetworkAccountController {
    @Resource
    private ConNetworkAccountMobileService conNetworkAccountMobileService;

    @ApiOperation("服务商注册账号")
    @PostMapping("/register")
    public Result register(@RequestBody @Valid NetworkRegisterAccountVo registerAccountVo) throws SMSException {
        conNetworkAccountMobileService.register(registerAccountVo);
        return Result.success("账号注册成功");
    }

    @ApiOperation("服务商登录")
    @PostMapping("/login")
    public Result login(@RequestBody @Valid LoginVo loginVo) throws SMSException, LoginException {
        AccountLoginToken token = conNetworkAccountMobileService.login(loginVo);
        return Result.success("登录成功").data(PageUtil.row(token));
    }

    @ApiOperation("服务商退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        String accessToken = TokenNetworkUtil.getToken(request);
        RedisNetworkLoginTokenUtil.removeAllTokenByAccessToken(accessToken);
        StpNetworkUtil.logoutByTokenValue(accessToken);
        return Result.success("退出登录成功");
    }

    @ApiOperation("注销账号")
    @GetMapping("/cancelAccount")
    public Result cancelAccount(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        conNetworkAccountMobileService.cancelAccount(loginInfo);
        return Result.success("账号注销成功！");
    }

    @ApiOperation("服务商刷新token")
    @GetMapping("/refreshToken")
    public Result refreshToken(@RequestParam @ApiParam(value = "刷新token", required = true) String refreshToken) throws LoginException {
        AccountLoginToken token = conNetworkAccountMobileService.refreshToken(refreshToken);
        return Result.success("token刷新成功").data(PageUtil.row(token));
    }

    @ApiOperation("修改登录页面的密码")
    @PutMapping("/logInRestPwd")
    public Result logInRestPwd(@RequestBody @Valid UserPasswordVo userPasswordVo) throws SMSException {
        conNetworkAccountMobileService.logInRestPwd(userPasswordVo);
        return Result.success("密码修改成功");
    }

    @ApiOperation("获取账号所属服务商信息")
    @GetMapping("/getNetworkInfo")
    public Result getNetworkInfo(HttpServletRequest request) {
        String accessToken = TokenNetworkUtil.getToken(request);
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(accessToken);
        NetworkMobileEmpVo empVo = conNetworkAccountMobileService.getByAccountId(loginInfo);
        RedisNetworkLoginTokenUtil.setAccountLoginInfoByAccessToken(accessToken, loginInfo);
        return Result.success().data(PageUtil.rows(empVo));
    }

    @ApiOperation("根据服务商编码选择服务商")
    @GetMapping("/getNetwork")
    public Result getNetwork(HttpServletRequest request, @RequestParam(required = false) @ApiParam(value = "服务商编码") String networkCode) {
        String accessToken = TokenNetworkUtil.getToken(request);
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(accessToken);
        conNetworkAccountMobileService.getNetwork(loginInfo, networkCode);
        RedisNetworkLoginTokenUtil.setAccountLoginInfoByAccessToken(accessToken, loginInfo);
        return Result.success("选择服务商成功");
    }

    @ApiOperation("获取当前登录账号信息")
    @GetMapping("/getAccountInfo")
    public Result getAccountInfo(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(conNetworkAccountMobileService.getAccountInfo(loginInfo)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "修改密码", addLogParam = false)
    @ApiOperation("修改密码")
    @PutMapping("/updatePwd")
    public Result updatePwd(HttpServletRequest request, @RequestBody @Valid UserPasswordVo userPasswordVo) throws SMSException {
        Long accountId = TokenNetworkUtil.getAccountIdByToken(request);
        conNetworkAccountMobileService.updatePwd(accountId, userPasswordVo);
        return Result.success("修改密码成功");
    }

    @ApiOperation("检查是否已绑定手机号")
    @GetMapping("/checkBindingPhone")
    public Result checkBindingPhone(HttpServletRequest request) {
        Long accountId = TokenNetworkUtil.getAccountIdByToken(request);
        return Result.success().data(PageUtil.row(conNetworkAccountMobileService.checkBindingPhone(accountId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "编辑个人资料", addLogParam = false)
    @ApiOperation("编辑个人资料")
    @PutMapping("/updateNetworkEmpInfo")
    public Result updateNetworkEmpInfo(HttpServletRequest request, @RequestBody UserMobileInfoVo infoVo) {
        Long accountId = TokenNetworkUtil.getAccountIdByToken(request);
        conNetworkAccountMobileService.updateNetworkEmpInfo(accountId, infoVo);
        return Result.success("个人资料编辑成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "绑定或换绑手机号", addLogParam = false)
    @ApiOperation("绑定或换绑手机号")
    @PutMapping("/bindingPhone")
    public Result bindingPhone(HttpServletRequest request, @RequestParam @ApiParam(value = "手机号", required = true) String phone,
                               @RequestParam @ApiParam(value = "验证码", required = true) String code) throws SMSException {
        Assert.isTrue(VerificUtil.phoneVerific(phone), "手机号格式错误");
        Assert.isTrue(StringUtils.isNotBlank(code), "验证码不能为空");

        Long accountId = TokenNetworkUtil.getAccountIdByToken(request);
        conNetworkAccountMobileService.bindingPhone(accountId, phone, code);
        return Result.success("绑定手机成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "新增或修改服务人员地址", addLogParam = false)
    @ApiOperation("新增或修改服务人员地址")
    @PostMapping("/saveNetworkEmpaddress")
    public Result saveNetworkEmpaddress(HttpServletRequest request, @RequestBody @Valid UserContactVo networkEmpaddress) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        conNetworkAccountMobileService.saveNetworkEmpaddress(loginInfo, networkEmpaddress);
        return Result.success(null == networkEmpaddress.getId()? "新增地址成功" : "修改地址成功");
    }

    @ApiOperation("获取服务人员地址列表")
    @PostMapping("/getNetworkEmpAddressList")
    public Result getNetworkEmpAddressList(HttpServletRequest request, @RequestBody PageUtil pageUtil) {
        Long networkEmpId = TokenNetworkUtil.getNetworkEmpId(request);
        return Result.success().data(conNetworkAccountMobileService.getNetworkEmpAddressList(networkEmpId, pageUtil));
    }

    @ApiOperation("根据地址ID获取地址信息")
    @GetMapping("/getNetworkEmpAddressDetail/{networkEmpAddressId}")
    public Result getNetworkEmpAddressDetail(@PathVariable @ApiParam(value = "服务人员地址ID", required = true) Long networkEmpAddressId) {
        return Result.success().data(PageUtil.row(conNetworkAccountMobileService.getNetworkEmpAddressDetail(networkEmpAddressId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.DELETE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "根据地址ID删除地址信息", addLogParam = false)
    @ApiOperation("根据地址ID删除地址信息")
    @DeleteMapping("/deleteNetworkEmpAddress/{networkEmpAddressId}")
    public Result deleteNetworkEmpAddress(@PathVariable @ApiParam(value = "服务人员地址ID", required = true) Long networkEmpAddressId) {
        conNetworkAccountMobileService.deleteNetworkEmpAddress(networkEmpAddressId);
        return Result.success("删除地址成功");
    }







}
